﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Data;
using System.Collections.Generic;
using System.Linq;
using System.Collections.ObjectModel;
using GeoFinderClientApp.Model;
using System.Device.Location;

namespace GeoFinderClientApp.Converters
{
    #region PushConverters
    public class PushOptionsConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return ((PushType)value).ToString() + " Notifications";
        }
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    public class PushDescriptionConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            switch ((PushType)value)
            {
                case PushType.Push: return "Turn on/off push notifications.";
                case PushType.Raw: return "Raw push notifications are used to send application specific information. The application must be running first.";
                case PushType.Tile: return "Tile push notifications update the application's tile displayed in the Start Screen. The application must be pinned by the user first.";
                case PushType.Toast: return "Toast push notifications are system-wide notifications that do not disrupt the user workflow or require intervention to resolve and are displayed in the top of the screen for ten seconds.";
                default: return "";
            }
        }
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
        #endregion

    #region TimeOuts
    public class TimeOutTypeConverter : IValueConverter
    {

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return ((TimeOutType)value).ToString() + " refresh timeout";
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    public class TimeOutDescriptionConverter : IValueConverter
    {

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            switch ((TimeOutType)value)
            {
                case TimeOutType.Facebook: return "Automatic friends list refresh.";
                case TimeOutType.GPS: return "Automatic GPS position refresh.";
                case TimeOutType.Server: return "Server-Client status automatically update.";
                default: return "";
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    #endregion

    #region SearchingArea
    public class SearchingAreaTypeConverter : IValueConverter
    {

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return "Searching area ray";
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    public class SearchingAreaDescriptionConverter : IValueConverter
    {

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return "The ray in meters around of you of the area where you are searching friends between " + SearchingAreaOption.MinSerarchingArea + " meters and " + SearchingAreaOption.MaxSerarchingArea + " meters";
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    #endregion

    public static class JsonIenumConverter
    {

        public static ObservableCollection<T> ToObservableCollection<T>(this IEnumerable<T> coll)
        {
            var c = new ObservableCollection<T>();
            foreach (var e in coll)
                c.Add(e);
            return c;
        }

        public static List<Friend> ConvertFriendsData(this IDictionary<string, object> data)
        {
            List<Friend> res = new List<Friend>();

            var list = (List<object>) data.First().Value;

            int i = 0;

            foreach (var item in list)
            {
                var tmp = (IDictionary<string, object>)item;
                var friend = new Friend() { ID = (long.Parse((string)tmp["id"])), Name = ((string)tmp["name"]), IsWhiteVisible = (i++) < 5 };
                res.Add(friend);
            }

            return res;
        }

        public static Friend ConvertMeData(this IDictionary<string, object> data)
        {
            return new Friend() { ID = (long.Parse((string)data["id"])), Name = ((string)data["name"]), IsWhiteVisible = true };
        }

    }

    public class GpsConverter : IValueConverter
    {
      public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
      {
        var coord = (GeoCoordinate)value;
        return coord.Latitude + ";" + coord.Longitude;
      }

      public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
      {
        var coord = (string)value;
        var tmp = coord.Split(new char[] { ';' });
        var lat = double.Parse(tmp[0]);
        var lon = double.Parse(tmp[1]);
        return new GeoCoordinate(lat, lon);
      }
    }
}
